package net.stuffrepos.bricknetcenter.plugins.bricklink;

import java.util.HashMap;
import java.util.Map;
import net.stuffrepos.bricknetcenter.database.schema.Information;
import net.stuffrepos.bricknetcenter.database.schema.Information.Type;

/**
 *
 * @author Eduardo H. Bogoni <eduardobogoni@gmail.com>
 */
public enum BrickLinkItemType {

    Set(Information.Type.Set, true),
    Part(Information.Type.Part, true),
    Minifig(Information.Type.Minifig, true),
    Book(Information.Type.Book, true),
    Gear(Information.Type.Gear, true),
    Catalog(Information.Type.Catalog, true),
    Instruction(Information.Type.Instruction, true),
    OriginalBox(Information.Type.OriginalBox, true),
    UnsortedLot(Information.Type.UnsortedLot, false);
    private Type informationType;
    private boolean catalogued;
    private final static Map<String, BrickLinkItemType> byCode;

    static {
        byCode = new HashMap<String, BrickLinkItemType>();

        for (BrickLinkItemType itemType : BrickLinkItemType.values()) {
            if (byCode.containsKey(itemType.getCode())) {
                throw new RuntimeException("Repeated code: " + itemType.getCode() + " - " + itemType);
            }

            byCode.put(itemType.getCode(), itemType);
        }
    }

    BrickLinkItemType(Information.Type informationType, boolean catalogued) {
        this.informationType = informationType;
        this.catalogued = catalogued;
    }

    public Information.Type getInformationType() {
        return informationType;
    }

    public String getCode() {
        return this.toString().substring(0, 1);
    }

    public boolean isCatalogued() {
        return catalogued;
    }

    public static BrickLinkItemType getByCode(String code) {
        if (byCode.containsKey(code)) {
            return byCode.get(code);
        } else {
            throw new RuntimeException("No item type found with code \"" + code + "\"");
        }
    }
}
